package org.springextensions.actionscript.mvcs.service.operation
{
	import flash.events.Event;
	
	import org.springextensions.actionscript.ioc.ResourceDefinition;
	import org.springextensions.actionscript.mvcs.command.AbstractLoadCompleteCommand;
	import org.springextensions.actionscript.mvcs.command.ICommand;

	public class FileLoadOperation extends ResourceDefinition
	{
		private var _url:String;
		
		public function get url():String 
		{
			return _url;
		}
		
		public function set url(value:String):void 
		{
			_url = value;
		}
		
		private var _loadCompleteAction:ICommand;
		
		protected function get loadCompleteAction():ICommand
		{
			return _loadCompleteAction;
		}
		
		private var _loaded:Boolean = false;
		
		override public function isComplete():Boolean
		{
			return _loaded;
		}
		
		override public function get progress():Number
		{
			return bytesLoaded / bytesTotal;
		}
		
		protected function get bytesLoaded():int 
		{
			return 0;
		}
		
		protected function get bytesTotal():int 
		{
			return 0;
		}
		
		public function FileLoadOperation(loadCompleteAction:AbstractLoadCompleteCommand)
		{
			super();
			
			loadCompleteAction.loadOperation = this;
			_loadCompleteAction = loadCompleteAction;			
		}
		
		protected function handleError(e:Event):void 
		{
			trace("file load error " + e);

			dispatchError(e);
		}
		
		protected function onLoadComplete(e:Event):void 
		{			
			trace("file: " + url + " load complete");
			_loadCompleteAction.execute();
			
			_loaded = true;
			
			dispatchResult(e);
		}
	}
}